package com.lm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lm.dao.TEntityTeachingMapper;
import com.lm.dao.TEntityTeachingSemesterMapper;
import com.lm.model.TEntityTeaching;
import com.lm.model.TEntityTeachingExample;
import com.lm.model.TEntityTeachingSemester;
import com.lm.service.TeachingService;
import com.lm.util.Guid;
import com.lm.util.SecurityUitil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName TeachingServiceImpl
 * @Description TODO
 * @Author wn
 * @Date 2019/3/31 16:38
 * @Version 1.0
 **/
@Service
@Transactional
public class TeachingServiceImpl implements TeachingService {
    @Autowired
    private TEntityTeachingMapper teachingMapper;
    @Autowired
    private TEntityTeachingSemesterMapper teachingSemesterMapper;

    @Override
    public PageInfo getList(Integer pageSize, Integer pageNumber, String planId) {
        TEntityTeachingExample example = new TEntityTeachingExample();
        example.createCriteria().andPlanIdEqualTo(planId);
        PageHelper.startPage(pageSize, pageNumber);
        return new PageInfo(teachingMapper.selectByExampleWithAll(example));
    }

    @Override
    public Integer addEntity(TEntityTeaching teaching) {
        String planId = teaching.getPlanId();
        String teachingId = Guid.get();
        List<TEntityTeachingSemester> teachingSemesters = teaching.getTeachingSemesters();
        for (TEntityTeachingSemester ts : teachingSemesters) {
            ts.setPlanId(planId);
            ts.setId(Guid.get());
            ts.setTeachingId(teachingId);
            teachingSemesterMapper.insert(ts);
        }
        teaching.setPlanId(planId);
        teaching.setId(teachingId);
        teaching.setCreateby(SecurityUitil.getCurrentUserId());
        teaching.setEditby(SecurityUitil.getCurrentUserId());
        teaching.setCreatetime(new Date());
        teaching.setEdittime(new Date());
        teaching.setStatue("1");
        return teachingMapper.insert(teaching);
    }
}
